<?php


namespace app\admin\controller\shop;


use app\common\controller\Adminbase;
use think\Db;

class Eshop extends Adminbase
{

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $_list = Db::name('shop')
                ->where('status', 'in', [0, 3])    //0待审核 3审核失败
                ->where('is_del', '=', 0)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            //获取数量
            $total = Db::name('shop')
//                ->where('status', 'in', [0, 3])    //0待审核 3审核失败
                ->where('is_del', '=', 0)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    //审核
    public function verify()
    {
        $business_id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
//            $insert_data = array(
//                'status' => $data['row']['status'],
//                'reason_refusal' => $data['row']['reason_refusal'],
//                'status_time'=>time()
//            );
            if ( !$data['row']['status']){
                $this->error('填写审核状态');
            }
            $data['row']['status_time']=time();
            $business_info = Db::name('shop')
                ->where('id', '=', $data['id'])
                ->update($data['row']);
            $this->success('操作成功');
        }
        $data = Db::name('shop')
            ->where('id', $business_id)
            ->find();
        $salesperson=Db::name('salesperson')->find($data['salesperson_id']);
        $data['salesperson'] =$salesperson['id'].'----'.$salesperson['name'];

        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();
        $city = Db::name('area')->where(['name' => $data['city']])->find();
        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });
        $this->assign([
            'data' => $data,
            'proviec' => $proviec_list,
            'city_list' => $city_list,
            'area_list' => $area_list
        ]);
        return $this->fetch();
    }
}